package tt.com.mourninghallmanager.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import tt.com.common.enums.Status;
import tt.com.common.model.Dead;
import tt.com.common.model.DeadHall;
import tt.com.common.model.Hall;
import tt.com.common.util.CommonResult;
import tt.com.common.util.PageUtil;
import tt.com.mourninghallmanager.mapper.IDeadHallMapper;
import tt.com.mourninghallmanager.service.DeadHallService;

import java.util.Date;
import java.util.List;

@Service
public class DeadHallImpl extends ServiceImpl<IDeadHallMapper, DeadHall> implements DeadHallService {

    @Autowired
    private IDeadHallMapper deadHallMapper;

    /**
     * 展示灵堂信息
     * @param i （0展示未进厅信息,1为展示以进厅信息,默认为0）
     * @param pageUtil 分页数据
     * @return
     */
    @Override
    public CommonResult showDeadHall(String i, PageUtil pageUtil) {
        List<DeadHall> list = baseMapper.selectList(null);
        pageUtil.setTotalSize(list.size());
        CommonResult result = null;
        String[] array = new String[list.size()];
        IPage<DeadHall> page = new Page<>(pageUtil.getPageIndex(),pageUtil.getPageSize());
        QueryWrapper<DeadHall> qw = new QueryWrapper<>();
        int j = 0;
        if (i.equals("0")){
            //获取所有灵堂id
            //0为还未分配灵堂 其余数字为分配的灵堂id
            for (DeadHall item : list){
                if (item.getHallId().equals("0") || item.getHallId().equals(null)){
                    array[j] = "1";
                    j++;
                    continue;
                }
                array[j] = item.getHallId();
                j++;
            }
            qw.notIn("hall_id",array).eq("data_status",1);
            IPage<DeadHall> DeadHall = baseMapper.selectPage(page,qw);
            pageUtil.setData(DeadHall);
            result = CommonResult.buildR(Status.OK,"返回所有未进厅信息",DeadHall);
        } else {
            qw.notIn("hall_id",0).eq("data_status",1);
            IPage<DeadHall> DeadHall = baseMapper.selectPage(page,qw);
            pageUtil.setData(DeadHall);
            result = CommonResult.buildR(Status.OK,"返回所有已进厅信息",DeadHall);
        }
        return result;
    }

    /**
     * 查询信息
     * @param deadHall
     * @param pageUtil
     * @return
     */
    @Override
    public CommonResult selectDeadHallMessage(DeadHall deadHall, PageUtil pageUtil) {
        List<DeadHall> list = baseMapper.selectList(null);
        pageUtil.setTotalSize(list.size());
        List<DeadHall> list1 = deadHallMapper.selectDeadHallMessage(deadHall,pageUtil.getPageIndex(),pageUtil.getPageSize());
        if (list1.size() == 0){
            return CommonResult.buildR(Status.DEFAULT,"查询结果为空");
        }else {
            return CommonResult.buildR(Status.OK,"查询成功，已返回查询结果",list1);
        }
    }

    /**
     * 伪删除业务信息
     * @param deadHall
     * @return
     */
    @Override
    public CommonResult deleteDeadHall(DeadHall deadHall) {
        deadHall.setDataStatus(0);
        if (baseMapper.updateById(deadHall)==1){
            return CommonResult.buildR(Status.OK,"删除成功");
        }else {
            return CommonResult.buildR(Status.OK,"删除失败");
        }
    }

    /**
     * 刷新页面 展示灵堂业务信息
     * @param pageUtil
     * @return
     */
    @Override
    public CommonResult refreshDeadHall(PageUtil pageUtil) {
        List<DeadHall> list = baseMapper.selectList(null);
        pageUtil.setPageSize(list.size());
        IPage<DeadHall> page = new Page<>(pageUtil.getPageIndex(),pageUtil.getPageSize());
        QueryWrapper<DeadHall> qw = new QueryWrapper<>();
        qw.eq("data_status",1);
        IPage<DeadHall> DeadHall = baseMapper.selectPage(page,qw);
        pageUtil.setData(DeadHall);
        return CommonResult.buildR(Status.OK,"刷新成功，已返回数据",pageUtil);
    }


    /**
     * 更新进厅时间
     * @param deadHall 获取对象id，根据对象id更新进厅时间
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult goDeadHall(DeadHall deadHall) {
        if (deadHall.getHallId()==""||deadHall.getHallId()==null){
            return CommonResult.buildR(Status.DEFAULT,"未分配灵堂,请先分配灵堂");
        }
        deadHall.setEnterHallTime(new Date());
        if (baseMapper.updateById(deadHall)==1){
            if (deadHallMapper.updateDeadHallStatusBy0(deadHall.getHallId())==1){
                return CommonResult.buildR(Status.OK,"已进厅");
            }else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return CommonResult.buildR(Status.OK,"回滚事务");
            }
        }else {
            return CommonResult.buildR(Status.DEFAULT,"数据修改失败");
        }
    }

    /**
     * 更新出厅时间
     * @param deadHall 获取对象id，根据对象id更新出厅时间
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult outDeadHall(DeadHall deadHall) {
        DeadHall d = baseMapper.selectById(deadHall.getId());
        d.setOutHallTime(new Date());
        if (deadHallMapper.updateDeadHallStatusBy1(d.getHallId())==1){
            d.setHallId("");
            if (baseMapper.updateById(d)==1){
                return CommonResult.buildR(Status.OK,"已出厅");
            }else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return CommonResult.buildR(Status.OK,"回滚事务");
            }
        }else {
            return CommonResult.buildR(Status.DEFAULT,"数据修改失败");
        }
    }

    /**
     * 修改灵堂业务信息
     * @param deadHall
     * @return
     */
    @Override
    public CommonResult updateDeadHall(DeadHall deadHall) {
        deadHall.setLastUpdateTime(new Date());
        if (baseMapper.updateById(deadHall)==1){
            return CommonResult.buildR(Status.OK,"修改成功");
        }else {
            return CommonResult.buildR(Status.OK,"修改失败");
        }
    }

    /**
     * 添加灵堂服务
     * @param deadHall
     * @return
     */
    @Override
    public CommonResult insertDeadHall(DeadHall deadHall) {
        List<DeadHall> list = baseMapper.selectList(null);
        deadHall.setId((list.size()+1)+"");
        deadHall.setCreateTime(new Date());
        deadHall.setLastUpdateTime(new Date());
        deadHall.setDataStatus(1);
        if (baseMapper.insert(deadHall)==1){
            return CommonResult.buildR(Status.OK,"添加成功");
        }else {
            return CommonResult.buildR(Status.OK,"添加失败");
        }
    }

    /**
     * 展示所有空闲灵堂信息 (启用状态 hall_status 0为未启用，1为已启用)
     * @param pageUtil 分页数据
     * @return
     */
    @Override
    public CommonResult changeFreeDeadHall(PageUtil pageUtil) {
        List<Hall> num = deadHallMapper.selectFreeDeadHallNum();
        pageUtil.setTotalSize(num.size());
        List<Hall> list = deadHallMapper.changeFreeDeadHall(pageUtil.getPageIndex(),pageUtil.getPageSize());
        if (list.size()==0){
            return CommonResult.buildR(Status.OK,"查询数据为空");
        }else {
            return CommonResult.buildR(Status.OK,"查询空闲灵堂成功",list);
        }
    }

    /**
     * 变更灵堂状态为告别（1为守灵 2为告别）
     * @param deadHall
     * @return
     */
    @Override
    public CommonResult changeHallTypeBySoul(DeadHall deadHall) {
        deadHall.setUseType(0);
        if (baseMapper.updateById(deadHall)==1){
            return CommonResult.buildR(Status.OK,"切换为守灵状态");
        }else {
            return CommonResult.buildR(Status.DEFAULT,"切换守灵状态失败");
        }
    }
    /**
     * 变更灵堂状态为守灵（1为守灵 2为告别）
     * @param deadHall
     * @return
     */
    @Override
    public CommonResult changeHallTypeByLeave(DeadHall deadHall) {
        deadHall.setUseType(1);
        if (baseMapper.updateById(deadHall)==1){
            return CommonResult.buildR(Status.OK,"切换为告别状态");
        }else {
            return CommonResult.buildR(Status.DEFAULT,"切换告别状态失败");
        }
    }

    /**
     * 逝者信息预登记
     * @param dead
     * @return
     */
    public CommonResult addHeadHumanMeassage(Dead dead) {
        CommonResult result = null;
        if (deadHallMapper.addHeadHumanMeassage(dead)==1){
            result = CommonResult.buildR(Status.OK,"逝者信息预登记成功！");
        }else {
            result = CommonResult.buildR(Status.DEFAULT,"逝者信息预登记失败！");
        }
        return result;
    }
}
